import 'package:flutter/material.dart';
import 'package:flutter_app/common/global.dart';
import 'package:http/http.dart' as http;

class UserInfoDialog extends Dialog {
  UserInfoDialog({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    String editUserName = Global.userName;
    String editUserEmail = Global.userEmail;

    return new Padding(
        padding: const EdgeInsets.all(12.0),
        child: Material(
          //创建透明层
          type: MaterialType.transparency, //透明类型(dialog的半透明效果)
          child: Center(
            //保证控件居中效果
            child: Container(
              padding: EdgeInsets.fromLTRB(10.0, 0, 10.0, 0),
              height: 180,
              child: Container(
                  decoration: ShapeDecoration(
                    color: Color(0xffffffff),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(
                        Radius.circular(8.0),
                      ),
                    ),
                  ),
                  child: Padding(
                    padding: EdgeInsets.all(15.0),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        TextFormField(
                          decoration: new InputDecoration(
                            hintText:
                                editUserName.length == 0 ? '用户名' : editUserName,
                          ),
                          onChanged: (value) {
                            editUserName = value ?? "";
                          },
                        ),
                        TextFormField(
                          decoration: new InputDecoration(
                            hintText: editUserEmail.length == 0
                                ? '邮箱'
                                : editUserEmail,
                          ),
                          onChanged: (value) => {editUserEmail = value ?? ""},
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: <Widget>[
                            new FlatButton(
                              onPressed: () async {
                                if (editUserName != Global.userName ||
                                    editUserEmail != Global.userEmail) {
                                  Global.editUserInfo(
                                      editUserName, editUserEmail);
                                  String paramStr =
                                      '{"id":"${Global.userId}","nickName":"${Global.userName}","Email":"${Global.userEmail}"}';
                                  await http.post(
                                      Global.serversUrl + "/EditUserInfo",
                                      body: paramStr);
                                }
                                Navigator.of(context).pop();
                              },
                              child: new Text("确认"),
                            ),
                            new FlatButton(
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                              child: new Text("取消"),
                            ),
                          ],
                        )
                      ],
                    ),
                  )),
            ),
          ),
        ));
  }
}
